" vim: ft=vim
" Test len()

Execute(Use __len__() if available);
let cls = {}
function! cls.__len__()
  return 12
endfunction

AssertEqual 12, object#len(cls)


Execute(Use len() of Vim if obj is a Collection or String);
let input = [
      \ range(10),
      \ '123',
      \ { 'a': 1, 'b': 2, 'c': 3},
      \ ]

for x in input
  AssertEqual len(x), object#len(x)
endfor


Execute(Throw TypeError if the len of obj cannot be determined);
let input = [
      \ 1,
      \ function('empty'),
      \ ]

if object#util#has_special_variables()
  call extend(input, [v:false, v:true, v:none, v:null,])
endif

if has('float')
  call add(input, 1.0)
endif

for X in input
  AssertThrows call object#len(g:X)
  Log g:vader_exception
  Assert g:vader_exception =~# 'TypeError'
endfor


Execute(Throw WrongType if __len__() returns non-number);
let cls = {}
function! cls.__len__()
  return 'bad'
endfunction

AssertThrows call object#len(g:cls)
Assert g:vader_exception =~# 'WrongType'
